<template>
  <!-- // :disabled="!editable|| editableFiexd['name']"  物品名称
 物品编码
:disabled="!editable|| editableFiexd['type']" 物品类型
:disabled="!editable|| editableFiexd['count']" 物品数量
:disabled="!editable|| editableFiexd['remark']" 物品说明
:disabled="!editable|| editableFiexd['status']"物品状态
 用于禁止-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="物品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['name', validatorRules.name]"
                :disabled="true"
                placeholder="请输入物品名称"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['code', validatorRules.code]"
                :disabled="!editable || editableFiexd['code']"
                placeholder="请输入物品编码"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物品类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                v-decorator="['type', validatorRules.status]"
                :trigger-change="true"
                dictCode="material_type"
                placeholder="请选择物品类型"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物品数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                type="number"
                v-decorator="['count', validatorRules.count]"
                placeholder="请输入物品数量"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物品说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea rows="4" v-decorator="['remark']" placeholder="请输入100个字符以内的说明" />
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="物品状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                v-decorator="['status', validatorRules.status]"
                :trigger-change="true"
                dictCode="material_status"
                placeholder="请选择物品状态"
              />
            </a-form-item>
          </a-col>

          <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col> -->
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
import XSearchUser from '@/components/extends/XSearchUser'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
export default {
  name: 'MaterialForm',
  components: {
    JFormContainer,
    JDate,
    JDictSelectTag,
    JSelectDepart,
    XSearchUser,
    XSelectUserByDep,
  },
  model: {
    prop: 'data',
    event: 'onchange',
  },
  props: {
    dataId: String,
    data: {
      type: Object,
      default: () => {
        return {}
      },
    },
    // editable: {
    //   type: Boolean,
    //   default: false,
    //   required: true,
    // },
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    editableFiexd: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      userDisabled: false,
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        code: {
          rules: [{ required: true, message: '请输入物品编码!' }],
        },
        name: {
          rules: [{ required: true, message: '请输入物品名称!' }],
        },
        status: {
          rules: [{ required: true, message: '请设置状态!' }],
        },
        type: {
          rules: [{ required: true, message: '请选择类型！' }],
        },
        count: {
          rules: [{ required: true, message: '请输入物品类型！' }],
        },
      },
      url: {
        add: '/material/add',
        edit: '/material/edit',
        queryById: '/material/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  watch: {
    dataId: {
      handler(nv, on) {
        if (nv && nv != ov) {
          this.model = {}
          this.getData()
        }
      },
    },
    data: {
      handler(nv, ov) {
        this.model = nv
      },
      immediate: true,
      deep: true,
    },
    model: {
      handler(nv, ov) {
        if (nv != ov) {
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(nv, 'code', 'name', 'count', 'remark', 'type', 'status'))
          })
          this.$emit('onchange', nv)
        }
      },
      immediate: true,
      deep: true,
    },
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
    console.log(this.editable)
  },
  methods: {
    getData(id) {
      let params = { id }
      getAction(this.url.queryById, params).then((res) => {
        if (res.success) {
          Object.assign(this.model, res.result)
        }
      })
    },
    add() {
      this.userDisabled = false
      this.open({})
    },
    edit(record) {
      this.userDisabled = true
      this.open(record)
    },
    open(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'code', 'name', 'count', 'remark', 'type', 'status'))
      })
      if (this.model.id) {
        this.getData(this.model.id)
      }
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.dataId) {
        this.getData(this.dataId)
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      return new Promise((resolve, reject) => {
        that.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!that.model.id) {
              httpurl += that.url.add
              method = 'post'
            } else {
              httpurl += that.url.edit
              method = 'put'
            }
            let formData = Object.assign(that.model, values)

            httpAction(httpurl, formData, method)
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                  resolve(res)
                } else {
                  that.$message.warning(res.message)
                  reject(res.message)
                }
              })
              .finally(() => {
                that.confirmLoading = false
              })
          } else {
            that.$message.error('信息有误请检查表单！')
          }
        })
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'code', 'id', 'name', 'count', 'status', 'type'))
    },
    editable() {
      console.log('123')
    },
  },
}
</script>

